Ext.define('DragonApp.view.foundation.ViewController', {
    extend: 'Ext.app.ViewController',

    requires: [
        'DragonApp.util.Msg',
        'DragonApp.util.Glyphs'
    ],

    //========
    //添加记录
    //========
    onAdd: function(button, e, options) {

        this.createDialog(null);
    },

    //========
    //编辑记录
    //========
    onEdit: function(button) {

        this.createDialog(button.getWidgetRecord());
    },

    //========
    //取消窗体
    //========
    onCancel: function(button, e, options) {

        var me = this;
        me.dialog = Ext.destroy(me.dialog);
    },
    //========
    //批量删除
    //========
    onBatDeleteExec: function(grid,url) {

        var store = grid.getStore();
        var records = grid.getSelectionModel().getSelection();

        //批量获取ID
        var arr = [];

        records.forEach(function(item) {
            arr.push(item.id);
        });

        var param = {};
        param.id = JSON.stringify(arr);

        this.batDeletePrompt(url, param, records, store);
    },
    //============
    //批量删除响应
    //============
    batDeletePrompt: function(url, param, records, store) {
        var me = this;

        Ext.Msg.show({
            title: '提示',
            msg: '删除所选记录?',
            buttons: Ext.Msg.YESNO,
            icon: Ext.Msg.QUESTION,
            fn: function(buttonId) {
                if (buttonId == 'yes') {
                    me.jsonpBatDelete(url, param, records, store);
                }
            }
        });
    },
    //===================
    //JsonP[批量删除]请求
    //===================
    jsonpBatDelete: function(url, param, records, store) {

        Ext.data.JsonP.request({
            url: url,
            async: false,
            params: param,
            callbackKey: "callback",
            success: function(result) {
                switch (result.success) {
                    case true:
                        DragonApp.util.Msg.showToast(result.msg);
                        store.remove(records);
                        break;

                    case false:
                        DragonApp.util.Msg.showToast(result.msg);
                        break;

                    default:
                        DragonApp.util.Msg.showToast(result.msg);
                        break;
                }
            },
            failure: function(result) {
                DragonApp.util.Msg.showToast(result);
            }
        });
    },

    //============
    //删除按钮响应
    //============
    deletePrompt: function(url, param, record) {
        var me = this;

        Ext.Msg.show({
            title: '提示',
            msg: '删除所选记录?',
            buttons: Ext.Msg.YESNO,
            icon: Ext.Msg.QUESTION,
            fn: function(buttonId) {
                if (buttonId == 'yes') {
                    me.jsonpDelete(url, param, record);
                }
            }
        });
    },
    //===============
    //JsonP[删除]请求
    //===============
    jsonpDelete: function(url, param, record) {

        Ext.data.JsonP.request({
            url: url,
            async: false,
            params: param,
            callbackKey: "callback",
            success: function(result) {
                switch (result.success) {
                    case true:
                        record.drop();
                        DragonApp.util.Msg.showToast(result.msg);
                        break;

                    case false:
                        DragonApp.util.Msg.showToast(result.msg);
                        break;
                }
            },
            failure: function(result) {
                DragonApp.util.Msg.showToast(result);
            }
        });
    }
});